//// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
//package com.linqingying.gitcode.pullrequest.data.service
//
//import com.intellij.openapi.progress.ProgressManager
//import git4idea.GitRemoteBranch
//import git4idea.remote.GitRemoteUrlCoordinates
//import com.linqingying.gitcode.api.GCGQLRequests
//import com.linqingying.gitcode.api.GCRepositoryCoordinates
//import com.linqingying.gitcode.api.GitCodeApiRequestExecutor
//import com.linqingying.gitcode.api.GitCodeApiRequests
//import com.linqingying.gitcode.api.data.GCLabel
//import com.linqingying.gitcode.api.data.GCRepositoryOwnerName
//import com.linqingying.gitcode.api.data.GCUser
//import com.linqingying.gitcode.api.data.GitCodeUserWithPermissions
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestRequestedReviewer
//import com.linqingying.gitcode.api.data.pullrequest.GCTeam
//import com.linqingying.gitcode.api.util.GitCodeApiPagesLoader
//import com.linqingying.gitcode.api.util.SimpleGCGQLPagesLoader
//import com.linqingying.gitcode.util.LazyCancellableBackgroundProcessValue
//import java.util.concurrent.CompletableFuture
//import java.util.function.BiFunction
//
//class GCPRRepositoryDataServiceImpl internal constructor(progressManager: ProgressManager,
//                                                         private val requestExecutor: GitCodeApiRequestExecutor,
//                                                         override val remoteCoordinates: GitRemoteUrlCoordinates,
//                                                         override val repositoryCoordinates: GCRepositoryCoordinates,
//                                                         private val repoOwner: GCRepositoryOwnerName,
//                                                         override val repositoryId: String,
//                                                         override val defaultBranchName: String?,
//                                                         override val isFork: Boolean)
//  : GCPRRepositoryDataService {
//
//  private val serverPath = repositoryCoordinates.serverPath
//  private val repoPath = repositoryCoordinates.repositoryPath
//
//  init {
//    requestExecutor.addListener(this) {
//      resetData()
//    }
//  }
//
//  private val collaboratorsValue = LazyCancellableBackgroundProcessValue.create(progressManager) { indicator ->
//    GitCodeApiPagesLoader
//      .loadAll(requestExecutor, indicator,
//               GitCodeApiRequests.Repos.Collaborators.pages(serverPath, repoPath.owner, repoPath.repository))
//  }
//
//  override val collaborators: CompletableFuture<List<GCUser>>
//    get() = collaboratorsValue.value.thenApply { list ->
//      list.map { GCUser(it.nodeId, it.login, it.htmlUrl, it.avatarUrl ?: "", null) }
//    }
//
//  private val teamsValue = LazyCancellableBackgroundProcessValue.create(progressManager) { indicator ->
//    if (repoOwner !is GCRepositoryOwnerName.Organization) emptyList()
//    else SimpleGCGQLPagesLoader(requestExecutor, {
//      GCGQLRequests.Organization.Team.findAll(serverPath, repoOwner.login, it)
//    }).loadAll(indicator)
//  }
//
//  override val teams: CompletableFuture<List<GCTeam>>
//    get() = teamsValue.value
//
//  override val potentialReviewers: CompletableFuture<List<GCPullRequestRequestedReviewer>>
//    get() = collaboratorsValue.value.thenCombine(teams,
//                                                 BiFunction<List<GitCodeUserWithPermissions>, List<GCTeam>, List<GCPullRequestRequestedReviewer>> { users, teams ->
//                                                   users
//                                                     .filter { it.permissions.isPush }
//                                                     .map { GCUser(it.nodeId, it.login, it.htmlUrl, it.avatarUrl ?: "", null) } +
//                                                   teams
//                                                 })
//
//  private val assigneesValue = LazyCancellableBackgroundProcessValue.create(progressManager) { indicator ->
//    GitCodeApiPagesLoader
//      .loadAll(requestExecutor, indicator,
//               GitCodeApiRequests.Repos.Assignees.pages(serverPath, repoPath.owner, repoPath.repository))
//      .map { GCUser(it.nodeId, it.login, it.htmlUrl, it.avatarUrl ?: "", null) }
//  }
//
//  override val issuesAssignees: CompletableFuture<List<GCUser>>
//    get() = assigneesValue.value
//
//  private val labelsValue = LazyCancellableBackgroundProcessValue.create(progressManager) { indicator ->
//    GitCodeApiPagesLoader
//      .loadAll(requestExecutor, indicator,
//               GitCodeApiRequests.Repos.Labels.pages(serverPath, repoPath.owner, repoPath.repository))
//      .map { GCLabel(it.nodeId, it.url, it.name, it.color) }
//  }
//
//  override val labels: CompletableFuture<List<GCLabel>>
//    get() = labelsValue.value
//
//  override fun resetData() {
//    collaboratorsValue.drop()
//    teamsValue.drop()
//    assigneesValue.drop()
//    labelsValue.drop()
//  }
//
//  override fun getDefaultRemoteBranch(): GitRemoteBranch? {
//    val currentRemote = repositoryMapping.remote
//    val currentRepo = currentRemote.repository
//    val branches = currentRepo.branches
//    if (defaultBranchName != null) {
//      return branches.findRemoteBranch("${currentRemote.remote.name}/$defaultBranchName")
//    }
//
//    return branches.findRemoteBranch("${currentRemote.remote.name}/master")
//           ?: branches.findRemoteBranch("${currentRemote.remote.name}/main")
//  }
//
//  override fun dispose() {
//    resetData()
//  }
//}
